<template>
  <el-card>
    <el-button type="primary" @click="$router.push('/role/add')" icon="el-icon-plus">添加菜单</el-button>
    <el-divider/>
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="id" label="ID" min-width="180"></el-table-column>
      <el-table-column prop="rolename" label="名称" min-width="180"></el-table-column>
      <el-table-column prop="status" label="状态" min-width="180">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.status==1">正常</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template v-slot="scope">
          <el-button circle type="primary" icon="el-icon-edit" size="mini"
          @click="$router.push(`/role/${scope.row.id}`)"
          ></el-button>
          <el-button
            circle
            type="warning"
            size="mini"
            icon="el-icon-view"
            @click="showInfo(scope.row.id)"
          ></el-button>
          <el-button circle type="danger" size="mini" icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗预览  -->
    <el-dialog title="角色信息预览" :visible.sync="dialogFormVisible">
      <el-form :model="roleInfo">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="roleInfo.rolename" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="权限节点" :label-width="formLabelWidth">
          <!-- 属性控件 -->
          <el-tree
            :data="menus"
            node-key="id"
            :default-expand-all="true"
            :default-checked-keys="roleInfo.menus.split(',')"
            show-checkbox
            :props="defaultProps"
            ref="tree"
          ></el-tree>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      dialogFormVisible: false,
      formLabelWidth: "120px",
      roleInfo: {
        menus: "",
        rolename: ""
      },
      menus: [],
      defaultProps: {
        children: "children",
        label: "title"
      }
    };
  },
  created() {
    //  获取角色列表
    this.getRoleList();
    // 获取树形菜单
    this.getMenuTree();
  },
  methods: {
    // 获取所有的权限列表
    getMenuTree() {
      this.$http.get("/api/menulist?istree=1").then(res => {
        if (res.code == 200) {
          this.menus = res.list;
        }
      });
    },
    // 显示模态框
    showInfo(id) {
      // 重置角色信息
      this.roleInfo = { menus: "" };
      // 显示模态框
      this.dialogFormVisible = true;
      // 获取当前角色信息
      this.getRoleInfo(id);
    },
    // 获取当前角色信息
    getRoleInfo(id) {
      this.$http.get(`/api/roleinfo?id=${id}`).then(res => {
        if (res.code == 200) {
          this.roleInfo = res.list;
          this.$nextTick(() => {
            // this.$refs.tree.setCheckedKeys(this.roleInfo.menus.split(','));
            this.$refs.tree.setCheckedKeys(this.roleInfo.menus.split(','));
          });
        }
      });
    },
    getRoleList() {
      this.$http.get("/api/rolelist").then(res => {
        if (res.code == 200) {
          this.list = res.list;
        }
      });
    }
  }
};
</script>

<style>
</style>